package fei.yuzhi.com.loveflowers.fragment.firstpagefragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import fei.yuzhi.com.loveflowers.R;
import fei.yuzhi.com.loveflowers.base.BaseFragment;
import fei.yuzhi.com.loveflowers.fragment.firstpagefragment.autoscroll.AutoScrollJumpActivity;
import fei.yuzhi.com.loveflowers.fragment.firstpagefragment.detail.DetailActivity;
import fei.yuzhi.com.loveflowers.fragment.firstpagefragment.search.SearchActivity;
import fei.yuzhi.com.loveflowers.fragment.firstpagefragment.searchresult.myview.CommonGallery;
import fei.yuzhi.com.loveflowers.fragment.firstpagefragment.searchresult.myview
        .Image3D_LunBo_Adapter;
import fei.yuzhi.com.loveflowers.utils.BannerLayout;
import fei.yuzhi.com.loveflowers.utils.ClickWay;
import fei.yuzhi.com.loveflowers.utils.MyGridView;
import fei.yuzhi.com.loveflowers.utils.MyListView;
import fei.yuzhi.com.loveflowers.zxing.android.CaptureActivity;

/**
 * 应用首页
 */
public class FirstPageFragment extends BaseFragment implements FirstPageContract.View {

    @BindView(R.id.firstpage_fragment_imageview)
    ImageView firstpageFragmentImageview;
    @BindView(R.id.firstpage_fragment_gideRv)
    MyGridView firstpageFragmentGideRv;
    @BindView(R.id.firstpage_fragment_timelimt)
    ImageView firstpageFragmentTimelimt;
    @BindView(R.id.firstpage_fragment_secondgideRv)
    MyGridView firstpageFragmentSecondgideRv;
    @BindView(R.id.firstpage_fragment_today)
    ImageView firstpageFragmentToday;
    @BindView(R.id.firstpage_fragment_threadgideRv)
    MyGridView firstpageFragmentThreadgideRv;
    @BindView(R.id.firstpage_fragment_hotsale)
    ImageView firstpageFragmentHotsale;
    @BindView(R.id.firstpage_fragment_lv)
    MyListView firstpageFragmentLv;
    @BindView(R.id.firstpage_fragment_scan)
    ImageView firstpageFragmentScan;
    @BindView(R.id.firstpage_fragment_search)
    TextView firstpageFragmentSearch;
    @BindView(R.id.mygallery)
    CommonGallery mygallery;
    //创建presenter的对象，用来调用view与model相联系的方法
    private FirstPagePresenter mPresenter;
    private Handler handler = new Handler();
    static final int firstGird = 1;
    static final int othergrid = 2;
    private int[] imageResIDs;
    private int index = 0;
    private Handler handler1=new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    mygallery.setSelection(index);
                    break;
            }
        }
    };
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
    Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_firstpager, container, false);
        ButterKnife.bind(this, view);
        mPresenter = new FirstPagePresenter(this);
        //调用presenter中的方法，将model中获取到的数据传递到view中
        mPresenter.getImageFromNet();
        mPresenter.getAutoScrollIamgeUrlFromNet();
        mPresenter.getGridRecyclerViewIamgeUrlFromNet();
        initSearch();
        imageResIDs=new int[]{R.mipmap.tu1,R.mipmap.tu2};
        Image3D_LunBo_Adapter image3D_lunBo_adapter = new Image3D_LunBo_Adapter(imageResIDs,getContext());
        mygallery.setAdapter(image3D_lunBo_adapter);
        mygallery.setSpacing(50); //图片之间的间距
        mygallery.setSelection((Integer.MAX_VALUE / 2) - (Integer.MAX_VALUE / 2) % imageResIDs.length);
        Timer timer = new Timer();
        timer.schedule(task, 3000, 3000);

        //绑定点击扫一扫跳转的页面
        firstpageFragmentScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), CaptureActivity.class);
                startActivity(intent);
            }
        });

        return view;
    }
    /**
     * 定时器，实现自动播放
     */
    private TimerTask task = new TimerTask() {
        @Override
        public void run() {
            Message message = new Message();
            message.what = 0;
            index = mygallery.getSelectedItemPosition();
            index++;
            handler1.sendMessage(message);
        }
    };

    private void initSearch() {
        //绑定搜索按钮的点击事件，跳转到另外一个新的界面进行搜索
        firstpageFragmentSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onAutoScrollSuccess(final List<String> urls, final List<String> mAutoScrollData) {
        //通过post方法，得到传递过来的网络数据
        handler.post(new Runnable() {
            @Override
            public void run() {


//                //网络地址
//                banner.setViewUrls(urls);
//                //添加点击监听
                mygallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent(getActivity(), AutoScrollJumpActivity.class);
                        intent.putExtra("data", mAutoScrollData.get(0));
                        startActivity(intent);
                    }
                });
            }
        });
    }

    @Override
    public void onAutoScrollFail(final String msg) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onImageViewSuccess(final String firstImageUrl, final String timeLimtImageUrl,
                                   final String todayImageUrl, final String hotSaleImageUrl,
                                   final List<String> horizontalRVImageUrl,
                                   final List<String> mListViewData) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                //如果获取网络地址成功，则利用glide将全球直采，限时秒杀等图片加载到控件中
                Glide.with(getActivity()).load(firstImageUrl).into(firstpageFragmentImageview);
                Glide.with(getActivity()).load(timeLimtImageUrl).into(firstpageFragmentTimelimt);
                Glide.with(getActivity()).load(todayImageUrl).into(firstpageFragmentToday);
                Glide.with(getActivity()).load(hotSaleImageUrl).into(firstpageFragmentHotsale);
                //将适配器的布局选择到othergrid，即选择到listview的布局中
                GridViewAdapter adapter = new GridViewAdapter(getActivity(),
                        horizontalRVImageUrl, othergrid);
                //对recyclerView进行适配
                firstpageFragmentLv.setAdapter(adapter);
                //绑定listview的单击事件
                firstpageFragmentLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long
                            id) {
                        //listview跳转界面的布局与自动轮播图的跳转页面相同，故同样跳转到此页面
                        Intent intent = new Intent(getActivity(), AutoScrollJumpActivity.class);
                        intent.putExtra("data", mListViewData.get(position));
                        startActivity(intent);
                    }
                });
            }
        });
    }

    @Override
    public void onImageViewFail(final String msg) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onGridRecyclerViewSuccess(final List<String> firstRvImageUrl, final List<String>
            secondGirdRvImageUrl, final List<String> threadRvImageUrl, final List<String>
                                                  mFirstRvData,
                                          final List<String> mSecondRvData) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                //对第一个网格布局的gridview进行适配
                GridViewAdapter firstAdapter = new GridViewAdapter(getActivity(),
                        firstRvImageUrl, firstGird);
                firstpageFragmentGideRv.setAdapter(firstAdapter);
                //对第二个GridView进行适配
                GridViewAdapter secondAdapter = new GridViewAdapter(getActivity(),
                        secondGirdRvImageUrl, othergrid);
                firstpageFragmentSecondgideRv.setAdapter(secondAdapter);
                ClickWay.getClick(firstpageFragmentSecondgideRv, getActivity(), DetailActivity
                        .class, mFirstRvData);
                //对第三个GridView进行适配
                GridViewAdapter threadAdapter = new GridViewAdapter(getActivity(),
                        threadRvImageUrl, othergrid);
                firstpageFragmentThreadgideRv.setAdapter(threadAdapter);
                ClickWay.getClick(firstpageFragmentThreadgideRv, getActivity(), DetailActivity
                        .class, mSecondRvData);
            }
        });
    }

    @Override
    public void onGirdRecyclerViewFail(final String msg) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

}
